package com.gxwy.changyongzhanghu.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.gxwy.changyongzhanghu.form.ChangYongZhangHu;
import com.gxwy.changyongzhanghu.service.ChangYongZhangHuService;

@Controller
@RequestMapping("/changyongzhanghu")
public class ChangYongZhangHuController {

	@Autowired
	private ChangYongZhangHuService contactService; 

	@RequestMapping("/lists")
	public String listContacts(Map<String, Object> map,HttpSession session) {

		map.put("contact", new ChangYongZhangHu());
		map.put("contactList", contactService.findAll());
      
	   session.setAttribute("list",null);
		return "skzhgl";
	}

	@RequestMapping(value = "/addC", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("contact")
	ChangYongZhangHu contact, BindingResult result,HttpSession session) {
        
		
		contactService.addContact(contact);
        return "redirect:lists";
	
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@ModelAttribute("contact")
	ChangYongZhangHu contact, BindingResult result,String name,Map<String, Object> map)
	{
	System.out.print(	contact.getZhuanruyinhang());

		if(name==""){
			 return "redirect:lists";
		}
		contactService.Update(contact,Integer.valueOf(name));
	
		 return "redirect:lists";

		
	}

	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public String deleteContact(String delet) {
		if(delet==""){
	    return "redirect:lists";
			
		}
		contactService.Delete(Integer.valueOf(delet));

		return  "redirect:lists";
	}
	@RequestMapping(value = "/cd",method = RequestMethod.POST)
	public String ContactId(@ModelAttribute("contact")ChangYongZhangHu contact,String id,Map<String, Object> map) {
		if(id==""){
			 return "redirect:lists";
		}
		map.put("contactLists",contactService.SelectId(Integer.valueOf(id)));

		return "redirect:lists";
	}
}
